package GUI;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client.ChatClient;
import Client.ChatClientSendThread;
import Mailbox.Mailbox;
import Mailbox.MailboxFTP;

public class GUIlogin extends JFrame implements ActionListener {

	private static final long serialVersionUID = 27L;
	private JPanel panel; // The panel
	private JButton loginButton; // The login button
	private JLabel chatsystemLabel1; // The label for the chat system
	private JLabel chatsystemLabel2; // The label for the chat system
	private JTextField usernameField; // Text field for the username
	private JLabel usernameLabel; // The label for the username

	private ChatClientSendThread client = null;
	private ChatClient clientRecieve;
	private GUIstartpage sp;
	private MailboxFTP mbftp;

	public GUIlogin(String dest) {
		panel = new JPanel();
		panel.setLayout(new GridBagLayout());

		loginButton = new JButton("Sign in!");
		chatsystemLabel1 = new JLabel("Piff & Puffs ");
		chatsystemLabel2 = new JLabel("ChatSystem");
		usernameField = new JTextField("", 10);
		usernameLabel = new JLabel("Username:");

		//String dest = ; // "130.235.35.39"; //OBS, kan behöva

		int port = 30005;
		Socket s = null;
		try {
			s = new Socket(dest, port);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,
					"Could not connect to the server. Please try again later!");
			System.exit(0);
		}
		mbftp = new MailboxFTP();
		client = new ChatClientSendThread(s, mbftp);
		clientRecieve = new ChatClient(s, client, mbftp);
		clientRecieve.start();

		loginButton.addActionListener(this); // Listens to the login button

		chatsystemLabel1.setFont(new Font("Dialog", Font.BOLD, 40));
		chatsystemLabel1.setForeground(new Color(242, 128, 161));
		chatsystemLabel2.setFont(new Font("Dialog", Font.BOLD, 40));
		chatsystemLabel2.setForeground(new Color(242, 128, 161));

		// The title
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 2;
		c.insets = new Insets(10, 0, 0, 0);
		panel.add(chatsystemLabel1, c);
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		c.insets = new Insets(10, 0, 50, 0);
		panel.add(chatsystemLabel2, c);

		// The username label
		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 0, 10, 0);
		panel.add(usernameLabel, c);

		// The username field
		c.gridx = 1;
		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_END;
		panel.add(usernameField, c);

		// The login button
		c.gridx = 0;
		c.gridy = 8;
		c.gridwidth = 4;
		c.insets = new Insets(20, 0, 0, 0);
		c.anchor = GridBagConstraints.PAGE_END;
		c.fill = GridBagConstraints.BOTH;
		c.ipady = 10;
		panel.add(loginButton, c);

		add(panel);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocation(200, 200);
		setSize(400, 400);
		setVisible(true);
		setResizable(false);

	}

	public String getClientUserName() {
		return usernameField.getText();
	}

	public static void main(String[] args) {
		if(args.length != 1){
			System.out.println("Ussage: Client.jar serverip");
			System.out.println("Exiting");
		}else{
			new GUIlogin(args[0]);
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String username = getClientUserName();
		if (username.equals("")) {
			JOptionPane
					.showMessageDialog(this, "Write your username!", null, 2);
		} else {
			this.setVisible(false);
			client.send("login:" + username);
			sp = new GUIstartpage(username, client, mbftp);
			clientRecieve.setSP(sp);
			client.send("getRooms");
			client.send("getAllNames");
		}
	}

}
